package top.kaoshanji.q1ch07a.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.kaoshanji.q1ch07a.model.Spitter;
import top.kaoshanji.q1ch07a.vo.SpitterForm;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/25 11:35
 */
@Controller
@RequestMapping("/spitter1")
public class SpitterController1 {

    @RequestMapping(value="/register", method= RequestMethod.GET)
    public String showRegistrationForm(Model model) {
        model.addAttribute("spitter" ,new Spitter());
        return "registerFormPage";
    }

    /**
     * 代码清单-q1ch07a-220页a：通过URL模板进行重定向
     * @param spitterForm SpitterForm
     * @param model Model
     * @return String
     */
    @RequestMapping(value="/register", method= RequestMethod.POST)
    public String processRegistration(SpitterForm spitterForm, Model model) {

        Spitter spitter = spitterForm.toSpitter();

        // 使用 Model 参数设置...传递简单类型
        model.addAttribute("userName", spitter.getUsername());
        model.addAttribute("spitterId", "101010"); // 查询参数
        return "redirect:/spitter1/spitterDetail/{userName}"; // 路径变量
    }

    @RequestMapping(value="/spitterDetail/{userName}", method=RequestMethod.GET)
    public String showSpitterProfile(@PathVariable("userName") String userName,
                                     @RequestParam("spitterId") long spitterId, Model model) {

        System.out.println("userName：" + userName);
        System.out.println("spitterId：" + spitterId);

        Spitter spitter = new Spitter();
        spitter.setUsername(userName);

        model.addAttribute("spitter", spitter);

        return "profilePage";
    }




}
